﻿namespace Util.ImageProcess

open System.Drawing
open System.Drawing.Imaging
open System.Drawing.Drawing2D
open System.IO
open Microsoft.FSharp

type ImageProcess =
    static  member private getEncoderInfo ( mimeType) :ImageCodecInfo =
            let  encoders=ImageCodecInfo.GetImageEncoders()
            let r =
                encoders
                |>Array.filter (fun encoder -> encoder.MimeType = mimeType )
            if r.Length>0 then r.[0]
            else null
    static member  private jpegEncoder =
        ImageProcess.getEncoderInfo "image/jpeg"

    static member DefaultQulityLevel =75L
    //static member Currying=
            
    //    FuncConvert.FuncFromTupled ImageProcess.CompressBitmap

    static member  CompressBitmap (imgInStream:Stream , imgOutStream:Stream,value:int64) =
        let myEncoder =Encoder.Quality
        let p = new EncoderParameter( myEncoder,value)
        let ps = new EncoderParameters(1)
        ps.Param.[0]<-p
        let bitmap = Bitmap.FromStream(imgInStream)
        if bitmap <> null then 
            bitmap.Save(imgOutStream,ImageProcess.jpegEncoder,ps)    

    //在保存的时候进行压缩
    static member  CompressBitmap (filePath:string,savePath:string,value:int64) :bool =
        ImageProcess.CompressBitmap( Bitmap.FromFile(filePath):?>Bitmap,savePath,value)
    static member  CompressBitmap (bitmap:Bitmap,savePath:string,value:int64) :bool =
        let myEncoder =Encoder.Quality
        let p = new EncoderParameter( myEncoder,value)
        let ps = new EncoderParameters(1)
        ps.Param.[0]<-p
        if bitmap <> null then 
            try
                bitmap.Save(savePath,ImageProcess.jpegEncoder,ps)
                true
            with
            | _ -> false
        else
            false
    static member  CompressBitmap (bitmap:Bitmap,outStream:Stream,value:int64) :bool =
        let myEncoder =Encoder.Quality
        let p = new EncoderParameter( myEncoder,value)
        let ps = new EncoderParameters(1)
        ps.Param.[0]<-p
        if bitmap <> null then 
            try
                bitmap.Save(outStream,ImageProcess.jpegEncoder,ps)
                true
            with
            | _ -> false
        else
            false
    static member  DefaultCompressBitmap(filePath:string, saveFilePath:string)  =
        ImageProcess.CompressBitmap(filePath, saveFilePath, ImageProcess.DefaultQulityLevel)
    static member  DefaultCompressBitmap(ins:Stream,outs:Stream)  =
        ImageProcess.CompressBitmap(ins,outs ,ImageProcess.DefaultQulityLevel)
    static member DefaultCompressBitmap(img:Bitmap,savePath:string)=
        ImageProcess.CompressBitmap(img,savePath,ImageProcess.DefaultQulityLevel)
    static member ScaleToAndSave( scale:float, img:Bitmap,savePath:string)=
        let w= (float img.Width) * scale
        let h = (float img.Height) * scale
        use canvas = new Bitmap( int w,int h)
        use g = Graphics.FromImage(canvas)
        g.SmoothingMode<-SmoothingMode.HighSpeed
        g.CompositingQuality<-CompositingQuality.HighSpeed
        g.CompositingMode<-CompositingMode.SourceCopy
        g.DrawImage( img ,new Rectangle(0,0,canvas.Width,canvas.Height))
        g.Flush()
        ImageProcess.DefaultCompressBitmap(canvas,savePath)
    ///看展示的地方传入level  在小的地方可以传
    static member GetLowerQulityImg(img:Bitmap,level : int,savePath:string )=
        ImageProcess.CompressBitmap(img, savePath,(int64 level))


//    static member GetLowerQulityImg(instream:Stream,level : int )=
//        let buff = new MemoryStream()
//        ImageProcess.CompressBitmap(instream, buff,(int64 level))
//        buff
//
//let LoadBitmap fileName =
//    Bitmap.FromFile(fileName) :?> Bitmap


//let CloneButLowerSize (pf:PixelFormat) (bitmap:Bitmap) =
//    let sizeF =bitmap.PhysicalDimension
//    bitmap.Clone(new RectangleF(new PointF(0.0f,0.0f),sizeF),pf)
//    //PixelFormat.Format16bppArgb1555 天空失真
//
//let Scale (persent:float32) (bitmap:Bitmap)=
//    bitmap.SetResolution( 100.0f,100.0f)//bitmap.HorizontalResolution*persent,bitmap.VerticalResolution*persent)
//    bitmap.Clone() :?> Bitmap

//
//let compress (scale:float) (bitmap:Bitmap) =
//  let newPic = new Bitmap( int ((float bitmap.Width)*scale), int ((float bitmap.Height) *scale))
//  newPic.SetResolution(bitmap.HorizontalResolution,bitmap.VerticalResolution)
//  use grap = Graphics.FromImage(newPic);
//  grap.SmoothingMode <- SmoothingMode.HighSpeed;
//  grap.InterpolationMode <- InterpolationMode.Low;
//  grap.CompositingQuality<-CompositingQuality.HighSpeed
//  grap.CompositingMode<- CompositingMode.SourceOver
//  //grap.DrawImage(bitmap, new Rectangle(0, 0, newPic.Width,newPic.Height),new Rectangle(0, 0, bitmap.Width,bitmap.Height),GraphicsUnit.Pixel)
//  grap.DrawImage(bitmap, new Rectangle(0, 0, newPic.Width,newPic.Height))
//  newPic

//
//let f = @"F:\YL\sucai\t8.jpg"
//let f_ = @"F:\YL\sucai\t8_p.jpg"
//let f = @"F:\YL\sucai\t2.jpg"
//let f_ = @"F:\YL\sucai\t2_p.jpg"

//let f = @"F:\YL\sucai\hr\[000054].jpg"
//let f_ = @"F:\YL\sucai\hr\[000054]_p.jpg"
//let f = @"F:\YL\sucai\hr\[000141].jpg"
//let f_ = @"F:\YL\sucai\hr\[000141]_p.jpg"
//let f = @"F:\YL\sucai\hr\[000198].jpg"
//let f_ = @"F:\YL\sucai\hr\[000198]_p.jpg"
//
//let test =
//    let bm = LoadBitmap f 
////bm|>CloneButLowerSize |> SaveBitmap <| f_
//    //bm |> compress 0.3  |>  SaveBitmap f_
//    bm|> SaveBitmap f_ 75L
//    bm.Dispose()
    //|>CloneButLowerSize PixelFormat.Format24bppRgb